home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Database How-To / Visual Basic 4 Database - How-to (The Waite Group)(1995).iso / textimp.fr_ / textimp.fr
Text File  |  1995-07-05  |  17KB  |  560 lines

  1. VERSION 4.00
  2. Begin VB.Form frmMain 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Text File Import"
  5.    ClientHeight    =   4455
  6.    ClientLeft      =   2280
  7.    ClientTop       =   1545
  8.    ClientWidth     =   4410
  9.    BeginProperty Font 
  10.       name            =   "MS Sans Serif"
  11.       charset         =   0
  12.       weight          =   700
  13.       size            =   8.25
  14.       underline       =   0   'False
  15.       italic          =   0   'False
  16.       strikethrough   =   0   'False
  17.    EndProperty
  18.    Height          =   4860
  19.    Left            =   2220
  20.    LinkTopic       =   "Form1"
  21.    ScaleHeight     =   4455
  22.    ScaleWidth      =   4410
  23.    Top             =   1200
  24.    Width           =   4530
  25.    Begin VB.CommandButton cmdListVendors 
  26.       Caption         =   "&List Vendors"
  27.       Height          =   375
  28.       Left            =   720
  29.       TabIndex        =   5
  30.       Top             =   1560
  31.       Width           =   2955
  32.    End
  33.    Begin VB.CommandButton cmdExit 
  34.       Cancel          =   -1  'True
  35.       Caption         =   "Exit"
  36.       Height          =   555
  37.       Left            =   1620
  38.       TabIndex        =   4
  39.       Top             =   3720
  40.       Width           =   1215
  41.    End
  42.    Begin VB.CommandButton cmdImport 
  43.       Caption         =   "&Import Data"
  44.       Height          =   375
  45.       Left            =   720
  46.       TabIndex        =   3
  47.       Top             =   1080
  48.       Width           =   2955
  49.    End
  50.    Begin VB.CommandButton cmdVendorDetails 
  51.       Caption         =   "&Vendor Details"
  52.       Height          =   375
  53.       Left            =   720
  54.       TabIndex        =   1
  55.       Top             =   2040
  56.       Width           =   2955
  57.    End
  58.    Begin VB.ListBox lstVendors 
  59.       Height          =   810
  60.       Left            =   480
  61.       Sorted          =   -1  'True
  62.       TabIndex        =   0
  63.       Top             =   180
  64.       Width           =   3495
  65.    End
  66.    Begin MSGrid.Grid grdInvoices 
  67.       Height          =   975
  68.       Left            =   240
  69.       TabIndex        =   2
  70.       Top             =   2580
  71.       Width           =   3915
  72.       _Version        =   65536
  73.       _ExtentX        =   6906
  74.       _ExtentY        =   1720
  75.       _StockProps     =   77
  76.       Cols            =   4
  77.       ScrollBars      =   2
  78.    End
  79. End
  80. Attribute VB_Name = "frmMain"
  81. Attribute VB_Creatable = False
  82. Attribute VB_Exposed = False
  83. Option Explicit
  84.  
  85. Private VendorFile As String
  86. Private InvoiceFile As String
  87. Private DatabaseFile As String
  88. Private PathToData As String
  89.  
  90. Private Sub cmdExit_Click()
  91.     End
  92. End Sub
  93. Private Sub cmdImport_Click()
  94.  
  95.     ' This procedure imports vendor and invoice text files named by the
  96.     ' form-level constants VendorFile and InvoiceFile, respectively, and
  97.     ' appends the records from them to the Vendors and Invoices tables in the
  98.     ' database named in the DatabaseFile constant.
  99.  
  100.     Dim lineOfData As String
  101.     Dim i As Integer
  102.     Dim db As DATABASE
  103.     Dim tblVendors As Recordset, tblInvoices As Recordset
  104.     Dim errorMsg As String
  105.  
  106.     ' The needRollback variable is used to flag the error handler as to
  107.     ' whether a Rollback is required. It is initially false and will be
  108.     ' set to true immediately after the BeginTrans statement.
  109.  
  110.     Dim needRollback As Boolean
  111.  
  112.     ' The mext two declarations each create a new Collection object and
  113.     ' assign a variable name to represent that object.
  114.  
  115.     Dim vendorsCollection As New Collection
  116.     Dim invoicesCollection As New Collection
  117.  
  118.     ' The next two declarations define variables that will be set to
  119.     ' class objects several places in this procedure.
  120.  
  121.     Dim ven As clsVendor
  122.     Dim inv As clsInvoice
  123.  
  124.     ' Set up the error handler.
  125.  
  126.     On Error GoTo ImportTextError
  127.  
  128.     ' Turn on the hourglass.
  129.  
  130.     Screen.MousePointer = 11
  131.  
  132.     ' Open the vendor file and read in the records a line at a time. Assign
  133.     ' each line to the variable lineOfData.
  134.  
  135.     Open VendorFile For Input As #1
  136.  
  137.     Do While Not EOF(1)
  138.  
  139.         Line Input #1, lineOfData
  140.  
  141.         ' Create a new clsVendor object and assign it to the variable ven.
  142.  
  143.         Set ven = New clsVendor
  144.  
  145.         ' Assign the line of data just read to the DelimitedString property of
  146.         ' the clsVendor object using the Property Let Tabbed String routine.
  147.  
  148.         ven.DelimitedString = lineOfData
  149.  
  150.         ' Add the new object to the vendors collection.
  151.  
  152.         vendorsCollection.Add ven
  153.  
  154.     Loop
  155.  
  156.     ' Close the vendor text file.
  157.  
  158.     Close #1
  159.  
  160.     ' Open the invoice file and read in the records a line at a time. Assign
  161.     ' each line to the variable lineOfData.
  162.  
  163.     Open InvoiceFile For Input As #1
  164.  
  165.     Do While Not EOF(1)
  166.  
  167.         Line Input #1, lineOfData
  168.  
  169.         ' Create a new clsInvoice object and assign it to the variable inv.
  170.  
  171.         Set inv = New clsInvoice
  172.  
  173.         ' Assign the line of data just read to the DelimitedString property of
  174.         ' the clsInvoice object using the Property Let Tabbed String routine.
  175.  
  176.         inv.DelimitedString = lineOfData
  177.  
  178.         ' Add the new object to the invoices collection.
  179.  
  180.         invoicesCollection.Add inv
  181.  
  182.     Loop
  183.  
  184.     ' Close the vendor text file.
  185.  
  186.     Close #1
  187.  
  188.     ' Open the database and the Vendors and Invoices tables.
  189.  
  190.     Set db = DBEngine.Workspaces(0).OpenDatabase(DatabaseFile)
  191.     Set tblVendors = db.OpenRecordset("Vendors", dbOpenTable)
  192.     Set tblInvoices = db.OpenRecordset("Invoices", dbOpenTable)
  193.  
  194.     ' We want to import all the records or none of the records. Therefore,
  195.     ' enclose the append operations in a transaction. Set the needRollback
  196.     ' variable to True to flag the error handler to execute a rollback if
  197.     ' an error occurs.
  198.  
  199.     BeginTrans
  200.     needRollback = True
  201.  
  202.     ' Take each item in the vendors collection and append it to the
  203.     ' vendors table.
  204.  
  205.     For i = 1 To vendorsCollection.Count
  206.  
  207.         Set ven = vendorsCollection.Item(i)
  208.         If ven.StoreNewItem(tblVendors) = False Or ven.Number = 0 Then
  209.  
  210.             ' The Jet engine returned an error when we tried to append the
  211.             ' record. Create a specific message informing the user of the
  212.             ' record that caused the error and the specific error that
  213.             ' occurred.
  214.  
  215.             errorMsg = "Error encountered importing vendor #"
  216.             errorMsg = errorMsg & LTrim(Str$(ven.Number)) & ": " & Error(Err)
  217.  
  218.             ' Branch to the error-handler which rolls back the transaction
  219.             ' and exits from the procedure.
  220.  
  221.             GoTo ImportTextError
  222.  
  223.         End If
  224.  
  225.     Next i
  226.  
  227.     ' Open the invoices table.
  228.  
  229.     For i = 1 To invoicesCollection.Count
  230.  
  231.         Set inv = invoicesCollection.Item(i)
  232.         If inv.StoreNewItem(tblInvoices) = False Or inv.vendorNumber = 0 Then
  233.  
  234.             ' The Jet engine returned an error when we tried to append the
  235.             ' record. Create a specific message informing the user of the
  236.             ' record that caused the error and the specific error that
  237.             ' occurred.
  238.  
  239.             errorMsg = "Error encountered importing invoice #"
  240.             errorMsg = errorMsg & inv.invoiceNumber & " for vendor #"
  241.             errorMsg = errorMsg & LTrim(Str$(ven.Number)) & ": " & Error(Err)
  242.  
  243.             ' Branch to the error-handler which rolls back the transaction
  244.             ' and exits from the procedure.
  245.  
  246.             GoTo ImportTextError
  247.  
  248.         End If
  249.  
  250.     Next i
  251.  
  252.     ' No error occurred during the append routine, so tell the Jet engine
  253.     ' to go ahead and commit the transaction.
  254.  
  255.     CommitTrans
  256.  
  257.     ' Restore the mouse pointer to its normal shape.
  258.  
  259.     Screen.MousePointer = 0
  260.  
  261. Exit Sub
  262.  
  263. ImportTextError:
  264.  
  265.     ' An error occurred during the import of the text files.
  266.  
  267.     ' Restore the mouse pointer to its normal shape.
  268.  
  269.     Screen.MousePointer = 0
  270.  
  271.     ' If no error message has been created in the body of the procedure,
  272.     ' create a default error message.
  273.  
  274.     If errorMsg = "" Then
  275.         errorMsg = "The following error has occurred: " & Error(Err)
  276.     End If
  277.  
  278.     ' Add the following to the error message so the user knows that the
  279.     ' original state of the Vendors and Invoices tables has not been changed.
  280.  
  281.     errorMsg = errorMsg & " No records have been added to the database."
  282.  
  283.     ' Display the error message. Since this error aborts the application,
  284.     ' use the critical error icon.
  285.  
  286.     MsgBox errorMsg, vbExclamation
  287.  
  288.     ' If the transaction had started, needRollback is True. Roll back the
  289.     ' transaction.
  290.  
  291.     If needRollback Then Rollback
  292.  
  293. Exit Sub
  294.  
  295. End Sub
  296. Private Sub cmdVendorDetails_Click()
  297.  
  298.     ' This procedure displays a form with information about the vendor
  299.     ' currently selected in the vendors list box.
  300.  
  301.     Dim db As DATABASE
  302.     Dim tbl As Recordset
  303.     Dim vendorNumber As Integer
  304.  
  305.     ' Set up the error handler.
  306.  
  307.     On Error GoTo VendorDetailsError
  308.  
  309.     If lstVendors.ListIndex > -1 Then
  310.  
  311.         ' The user has selected a vendor. Get the vendor number from the
  312.         ' list box ItemData property and assign it to a variable.
  313.  
  314.         vendorNumber = lstVendors.ItemData(lstVendors.ListIndex)
  315.  
  316.         ' Open the database and the Vendors table. Open the database
  317.         ' read-only since we only plan to read from it.
  318.  
  319.         Set db = DBEngine.Workspaces(0).OpenDatabase(DatabaseFile, False, True)
  320.         Set tbl = db.OpenRecordset("Vendors", dbOpenTable)
  321.  
  322.         ' Set the index to the primary key (the vendor number), find the
  323.         ' desired record, and gently remind the database engine to unlock
  324.         ' the index.
  325.  
  326.         tbl.Index = "PrimaryKey"
  327.         tbl.Seek "=", vendorNumber
  328.         DBEngine.Idle dbFreeLocks
  329.  
  330.         ' Read the vendor information from the database into the controls
  331.         ' of the vendor details form.
  332.  
  333.         frmVendorDetails.lblNumber = tbl("Vendor Number")
  334.         If Not IsNull(tbl("Name")) Then frmVendorDetails.lblName = _
  335.             tbl("Name") Else frmVendorDetails.lblName = ""
  336.         If Not IsNull(tbl("Address")) Then frmVendorDetails.lblAddress = _
  337.             tbl("Address") Else frmVendorDetails.lblAddress = ""
  338.         If Not IsNull(tbl("FEIN")) Then frmVendorDetails.lblFEIN = _
  339.             tbl("FEIN") Else frmVendorDetails.lblFEIN = ""
  340.  
  341.         ' Close the table and display the vendor details form as a modal form.
  342.  
  343.         tbl.Close
  344.         frmVendorDetails.Show 1
  345.  
  346.     Else
  347.  
  348.         ' The user clicked Vendor Details without first selecting a vendor.
  349.  
  350.         Beep
  351.         MsgBox "You haven't selected a vendor", vbExclamation
  352.  
  353.     End If
  354.  
  355. Exit Sub
  356.  
  357. VendorDetailsError:
  358.  
  359.     ' An error has occurred. Inform the user of the error and exit from the
  360.     ' procedure.
  361.     
  362.     MsgBox Error(Err)
  363.  
  364. Exit Sub
  365.  
  366. End Sub
  367. Private Sub Form_Load()
  368.  
  369.     Dim db As DATABASE
  370.  
  371.     ' Assign fully qualified pathnames to the form level data file variables.
  372.     PathToData = DataPath()
  373.     VendorFile = PathToData & "\CHAPTER.04\VENDORS.DAT"
  374.     InvoiceFile = PathToData & "\CHAPTER.04\INVOICES.DAT"
  375.     DatabaseFile = PathToData & "\CHAPTER.04\ACCTSPAY.MDB"
  376.  
  377.     ' Initialize the grid control.
  378.     InitializeGrid
  379.  
  380.     ' Delete any existing data in the Vendors and Invoices tables.
  381.     Set db = DBEngine.Workspaces(0).OpenDatabase(DatabaseFile)
  382.     db.Execute ("DELETE Vendors.* from Vendors")
  383.     db.Execute ("DELETE Invoices.* from Invoices")
  384.  
  385.  
  386. End Sub
  387. Private Sub InitializeGrid()
  388.  
  389.     Const GRID_ALIGNLEFT = 0
  390.     Const GRID_ALIGNRIGHT = 1
  391.     Const GRID_ALIGNCENTER = 2
  392.  
  393.     ' Set up the column widths for the grid. We're not using column 0,
  394.     ' so set its width to one pixel (the minimum allowable).
  395.  
  396.     grdInvoices.ColWidth(0) = 1
  397.     grdInvoices.ColWidth(1) = 1200
  398.     grdInvoices.ColWidth(2) = 1200
  399.     grdInvoices.ColWidth(3) = 1200
  400.  
  401.     ' Set column alignments.
  402.  
  403.     grdInvoices.ColAlignment(1) = GRID_ALIGNLEFT
  404.     grdInvoices.ColAlignment(2) = GRID_ALIGNCENTER
  405.     grdInvoices.ColAlignment(3) = GRID_ALIGNRIGHT
  406.     grdInvoices.FixedAlignment(1) = GRID_ALIGNLEFT
  407.     grdInvoices.FixedAlignment(2) = GRID_ALIGNCENTER
  408.     grdInvoices.FixedAlignment(3) = GRID_ALIGNRIGHT
  409.  
  410.     ' Insert the column titles in the top row of the grid.
  411.  
  412.     grdInvoices.Row = 0
  413.     grdInvoices.Col = 1
  414.     grdInvoices.TEXT = "Inv #"
  415.     grdInvoices.Col = 2
  416.     grdInvoices.TEXT = "Date"
  417.     grdInvoices.Col = 3
  418.     grdInvoices.TEXT = "Amount"
  419.  
  420.     ' Initialize the grid to show only the title row.
  421.  
  422.     grdInvoices.Rows = 1
  423.  
  424. End Sub
  425. Private Sub FillInvoiceList(vendor As Integer)
  426.  
  427.     ' This procedure is called when the user clicks on a vendor name.
  428.     ' It fills the grid with the invoices for that vendor currently in the
  429.     ' data base. The vendor argument is the vendor number of the selected
  430.     ' vendor.
  431.  
  432.     Dim db As DATABASE
  433.     Dim snap As Recordset
  434.     Dim rownum As Integer
  435.     Dim sql As String
  436.  
  437.     ' Create a new collection for invoices and a variable to assign
  438.     ' individual invoices to.
  439.  
  440.     Dim invoicesCollection As New Collection
  441.     Dim inv As clsInvoice
  442.  
  443.     ' Set up the error handler.
  444.  
  445.     On Error GoTo FillInvoiceListError
  446.  
  447.     ' Open the database and create a snapshot consisting of the invoice
  448.     ' records for the vendor passed as the argument. Open the database
  449.     ' read-only since we only need to read records.
  450.  
  451.     Set db = DBEngine.Workspaces(0).OpenDatabase(DatabaseFile, False, True)
  452.     sql = "SELECT [Invoice Number] FROM Invoices"
  453.     sql = sql & " WHERE [Vendor Number] = " & vendor
  454.     Set snap = db.OpenRecordset(sql, dbOpenSnapshot)
  455.  
  456.     If snap.RecordCount > 0 Then
  457.  
  458.         ' At least one record was retrieved, so process each record.
  459.  
  460.         snap.MoveFirst
  461.         Do While Not snap.EOF
  462.  
  463.             ' Create a new clsInvoice object and assign it to a variable.
  464.  
  465.             Set inv = New clsInvoice
  466.  
  467.             ' Use the Retrieve method of clsInvoice to get the invoice
  468.             ' from the database and add it to the invoices collection.
  469.  
  470.             If inv.Retrieve(db, vendor, snap("Invoice Number")) Then
  471.                 invoicesCollection.Add inv
  472.             End If
  473.  
  474.             snap.MoveNext
  475.  
  476.         Loop
  477.  
  478.         ' Set the number of rows in the grid to the number of invoices
  479.         ' retrieved. plus one for the headings row.
  480.  
  481.         grdInvoices.Rows = invoicesCollection.Count + 1
  482.  
  483.         ' Cycle through the invoices collection. Assign each to the
  484.         ' clsInvoice variable inv and use the class's AddToGrid method to
  485.         ' add the invoice to the grid.
  486.  
  487.         For rownum = 1 To invoicesCollection.Count
  488.             Set inv = invoicesCollection.Item(rownum)
  489.             inv.AddToGrid grdInvoices, rownum
  490.         Next rownum
  491.  
  492.     Else
  493.  
  494.         ' The snapshot is empty; there are no invoices in the database for
  495.         ' this user. Set the grid to show just the header row.
  496.  
  497.         grdInvoices.Rows = 1
  498.  
  499.     End If
  500.  
  501. Exit Sub
  502.  
  503. FillInvoiceListError:
  504.  
  505.     ' Display the standard error message, reset the grid to show just the
  506.     ' heading row, and deselect the vendor's name in the vendor list.
  507.  
  508.     grdInvoices.Rows = 1
  509.     lstVendors.ListIndex = -1
  510.     MsgBox Error(Err)
  511.  
  512. Exit Sub
  513.  
  514. End Sub
  515. Private Sub lstVendors_Click()
  516.  
  517.     ' Get the selected vendor's vendor number from the selected item's
  518.     ' ItemData value, and pass that value to the FillInvoiceList routine,
  519.     ' which fills the grid with invoices from this vendor.
  520.  
  521.     FillInvoiceList lstVendors.ItemData(lstVendors.ListIndex)
  522.  
  523. End Sub
  524. Private Sub cmdListVendors_Click()
  525.  
  526.     ' This procedure gets a list of vendors from the database and displays
  527.     ' it in the list box.
  528.  
  529.     Dim db As DATABASE
  530.     Dim tbl As Recordset
  531.  
  532.     ' Set up the error handler.
  533.  
  534.     On Error GoTo ListVendorsError
  535.  
  536.     ' Open the database and Vendors table. Open the database read-only since
  537.     ' reading is all we need to do.
  538.  
  539.     Set db = DBEngine.Workspaces(0).OpenDatabase(DatabaseFile, False, True)
  540.     Set tbl = db.OpenRecordset("Vendors", dbOpenTable)
  541.     tbl.MoveFirst
  542.     Do While Not tbl.EOF
  543.         lstVendors.AddItem tbl("Name")
  544.         lstVendors.ItemData(lstVendors.NewIndex) = tbl("Vendor Number")
  545.         tbl.MoveNext
  546.     Loop
  547.     tbl.Close
  548.  
  549. Exit Sub
  550.  
  551. ListVendorsError:
  552.  
  553.     ' Inform the user of the error, clear the vendors list, and exit from
  554.     ' the procedure.
  555.  
  556.     lstVendors.Clear
  557.     MsgBox Error(Err)
  558.  
  559. End Sub
  560.